package com.shuimin.service.service.sys;

import com.shuimin.service.App;
import com.shuimin.service.model.sys.User;
import pond.common.S;
import pond.common.STRING;
import pond.db.JDBCTmpl;
import pond.db.sql.Criterion;
import pond.db.sql.Sql;
import pond.db.sql.SqlSelect;
import pond.db.sql.SqlWhere;
import pond.security.rbac.RBAC;

import java.util.List;

/**
 * Created by ed on 12/24/15.
 */
public class SystemServices {

  public static boolean validateLoginUser(User user, String pass) {
    return user != null && user.get("password").equals(pass);
  }

  public static boolean userIsSuperAdmin(JDBCTmpl t, String id) {
    S._assert(t);
    return (t.count("SELECT * FROM rbac_user_has_role WHERE user_id = ?", id) > 0);
  }

  public static <T extends Sql> SqlWhere<T> addShopFilter(
      SqlWhere<T> sqlSelect,
      String shop_id_name,
      String shop_id) {

    S._assertNotNull(sqlSelect, shop_id_name);
    RBAC.Roles roles = App.currentRoles();

    List<String> allowedShops =
        S._for(ShopService.getByUser(App.currentUser().id())).map(s -> s.id()).toList();

//    S.echo("allowed_shops", );

    if (roles.hasAny(SysRoles.Roles.QUERY_ACROSS_SHOPS.id)) {
      //拥有全局访问权
      if (STRING.notBlank(shop_id)) {
        //指定门店
        sqlSelect.where(shop_id_name, Criterion.EQ, shop_id);
      } else {
//        //合并门店
//        if (allowedShops.size() > 0)
//          sqlSelect.where(shop_id_name, Criterion.IN, allowedShops.toArray(new String[allowedShops.size()]));
      }
    } else if (roles.hasAny(SysRoles.Roles.QUERY_ACROSS_SHOPS_FOR_BRAND.id)) {
      //拥有跨门店访问权限
      if (STRING.notBlank(shop_id)) {
//          sqlSelect.where(shop_id_name, Criterion.EQ, App.currentUser().shop().id());

        if (allowedShops.contains(shop_id))
          sqlSelect.where(shop_id_name, Criterion.EQ, shop_id);
        else
          sqlSelect.where(shop_id_name, Criterion.EQ, App.currentUser().shop().id());

      } else {
        //合并门店
        if (allowedShops.size() > 0)
          sqlSelect.where(shop_id_name, Criterion.IN, allowedShops.toArray(new String[allowedShops.size()]));
        else
          sqlSelect.where(shop_id_name, Criterion.EQ, App.currentUser().shop().id());
      }
    } else {
      //普通权限，只能查看自己所在门店
      sqlSelect.where(shop_id_name, Criterion.EQ, App.currentUser().shop().id());
    }
    return sqlSelect;
  }

  /**
   * 按照指定顺序排序(seq)
   * @param sqlSelect
   * @param shop_seq_name
   * @param flag true--正序,false--逆序
   * @return
   */
  public static SqlSelect addShopSeq(SqlSelect sqlSelect,
                                                       String shop_seq_name,
                                                       boolean flag){

    if (App.currentRoles().hasNone(SysRoles.Roles.SHOP_MGMT.id)) {
      //管理门店是0，不显示门店为-1,其他门店为正数,必须拥有管理门店权限才能看到所有
      sqlSelect.where(shop_seq_name, Criterion.GREATER_THAN, "0");
    }
    if(flag){
      sqlSelect.orderBy(" "+shop_seq_name+" ASC");
    }else{
      sqlSelect.orderBy(" "+shop_seq_name+" DESC");
    }

    return sqlSelect;
  }

}
